class Task
  include Mongoid::Document
  include Mongoid::Timestamps::Created
  include Mongoid::MultiParameterAttributes # for update of due

  belongs_to :competition
  belongs_to :author, class_name: "User", inverse_of: :tasks_author
  belongs_to :assigned_to, class_name: "User", inverse_of: :tasks_assigned_to

  field :title, type: String
  field :due, type: Time
  field :finished_at, type: Time

  has_one :topic, autobuild: true, dependent: :destroy, as: :topic_container

  has_and_belongs_to_many :tags, inverse_of: nil
  
  after_save do |doc|
    if doc.topic.new_record?
      doc.topic.save
    end
  end

  scope :unfinished, where(finished_at: nil, :due.ne => nil)
  scope :finished, where(:finished_at.ne => nil, :due.ne => nil)
  scope :unspecified, where(due: nil)
end
